﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Common
{


    public class Cls_Speeds : BaseNode
    {
        public Cls_Speeds(XmlNode Node)
            : base(Node)
        {
            if (Node == null)
            {
                throw new XmlException("Node is equal null");
            }
            _XmlNode = Node;
        }


        private List<Cls_Speed> _list = null;

        public List<Cls_Speed> Speeds
        {
            get
            {
                // List<BaseNode> lst = new List<BaseNode>();
                Cls_Speed c = null;

                if (null != _list)
                    _list.Clear();
                else
                    _list = new List<Cls_Speed>();

                foreach (XmlNode x in _XmlNode.SelectNodes("Speed"))
                {
                    c = new Cls_Speed(x);
                    //  BaseNode b = c as BaseNode;
                    _list.Add(c);
                }
       
                _list.Sort();
                return _list;
            }
        }


        public Cls_Speed Speed(int id)
        {
            XmlNode ch;
            XmlDocument doc = this._XmlNode.OwnerDocument;

            //add new task
            ch = _XmlNode.SelectSingleNode("Speed[@id=" + id.ToString() + "]");
            if (ch == null) return null;

            return new Cls_Speed(ch);
        }

        public Cls_Speed AddSpeed(int id)
        {
            //find 
            XmlNode ch;
            XmlDocument doc = this._XmlNode.OwnerDocument;
            Cls_Speed ExistCh = Speed(id);
            if (null != ExistCh) return ExistCh;

            //add new task
            ch = _XmlNode.AppendChild(doc.CreateElement("Speed"));
            ch.Attributes.Append(doc.CreateAttribute("id")).Value = id.ToString();

            ///转速id
            ch.AppendChild(doc.CreateElement("KeyPhaseID")).InnerText = (id + 1).ToString();

            ///转速名称  
            ch.AppendChild(doc.CreateElement("KeyPhaseName")).InnerText = "转速" + (id + 1);
            ///安装角度
            ch.AppendChild(doc.CreateElement("KeyPhaseAngle")).InnerText = "0";
            ///旋转方向
            ch.AppendChild(doc.CreateElement("RotationDirection")).InnerText = "0";

            return new Cls_Speed(ch);
        }



        public void UpdateSpeed(int id, string name, string data)
        {
            Cls_Speed ch = this.Speed(id);
            if (null == ch) return;
            XmlNode n = ch.Node.ParentNode;
            ch.Node.SelectSingleNode(name).InnerText = data;
        }



        public void DeleteSpeed(int id)
        {
            Cls_Speed ch = this.Speed(id);
            if (null == ch) return;

            XmlNode n = ch.Node.ParentNode;
            n.RemoveChild(ch.Node);
        }

    }
    public class Cls_Speed : BaseNode
    {
        public Cls_Speed(XmlNode Node)
            : base(Node)
        {
            if (Node == null)
            {
                throw new XmlException("Node is equal null");
            }
            _XmlNode = Node;
        }



        ///键相ID
        public string KeyPhaseID
        {
            get
            {
                if (_XmlNode == null)
                {
                    return "";
                }
                return _XmlNode["KeyPhaseID"].InnerText;
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("KeyPhaseID");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("KeyPhaseID"));
                Node.InnerText = value.ToString();
            }
        }


        ///转速名称 
        public string KeyPhaseName
        {
            get
            {
                if (_XmlNode == null)
                {
                    return "";
                }
                return _XmlNode["KeyPhaseName"].InnerText;
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("KeyPhaseName");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("KeyPhaseName"));
                Node.InnerText = value.ToString();
            }
        }


        ///安装角度
        public string KeyPhaseAngle
        {
            get
            {
                if (_XmlNode == null)
                {
                    return "";
                }
                return _XmlNode["KeyPhaseAngle"].InnerText;
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("KeyPhaseAngle");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("KeyPhaseAngle"));
                Node.InnerText = value.ToString();
            }
        }


        ///旋转方向
        public string RotationDirection
        {
            get
            {
                if (_XmlNode == null)
                {
                    return "";
                }
                return _XmlNode["RotationDirection"].InnerText;
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("RotationDirection");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("RotationDirection"));
                Node.InnerText = value.ToString();
            }
        }
    }
}
